package com.xm.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
    @Value("${redis.nodes}")
    private String nodes;

    @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> hostAndPorts=new HashSet<>();
        String[] nodeArray=nodes.split(",");
        for(String node:nodeArray){
            String host=node.split(":")[0];
            Integer port=Integer.parseInt(node.split(":")[1]);
            hostAndPorts.add(new HostAndPort(host,port));
        }
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(40);
        jedisPoolConfig.setMinIdle(10);
        jedisPoolConfig.setMaxTotal(1000);
        return new JedisCluster(hostAndPorts,jedisPoolConfig);
    }
}
